package jcu.ict.profaid;

import android.database.Cursor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.sun.corba.se.spi.ior.MakeImmutable;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint({ "NewApi", "SimpleDateFormat" })
public class FormAddQuestion extends Activity {
	@SuppressLint("NewApi")
	private static QuestionDbAdapter qdb;
	private static AnswerDbAdapter adb;
	private String title;
	private String date;
	private String question;
	private String answer_time = "20";
	private String course_id;
	private String answer1;
	private String answer2;
	private String answer3;
	private String answer4;
	private String answer5;
	private Cursor cursor;
	private int hour;
	private int minute;
	private int second;
	private java.util.Date d1;
	private SimpleDateFormat format;
	private EditText edt_title;
	private EditText edt_question;
	private EditText edt_answer_1;
	private EditText edt_answer_2;
	private EditText edt_answer_3;
	private EditText edt_answer_4;
	private EditText edt_answer_5;
	private TextView txtA;
	private TextView txtB;
	private TextView txtC;
	private TextView txtD;
	private TextView txtE;
	private String Question_id;
	private String user_id;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.form_add_question);

		Bundle bundle = this.getIntent().getExtras();

		course_id = bundle.getString("setIdCourse");
		user_id = (String) bundle.getCharSequence("setUserId");

		edt_title = (EditText) findViewById(R.id.edtTitle);
		edt_question = (EditText) findViewById(R.id.edtQuestion);
		edt_answer_1 = (EditText) findViewById(R.id.edtAnswer1);
		edt_answer_2 = (EditText) findViewById(R.id.edtAnswer2);
		edt_answer_3 = (EditText) findViewById(R.id.edtAnswer3);
		edt_answer_4 = (EditText) findViewById(R.id.edtAnswer4);
		edt_answer_5 = (EditText) findViewById(R.id.edtAnswer5);

		txtA = (TextView) findViewById(R.id.txtA);
		txtB = (TextView) findViewById(R.id.txtB);
		txtC = (TextView) findViewById(R.id.txtC);
		txtD = (TextView) findViewById(R.id.txtD);
		txtE = (TextView) findViewById(R.id.txtE);

		qdb = new QuestionDbAdapter(getBaseContext());
		adb = new AnswerDbAdapter(getBaseContext());

		Button btnSave = (Button) findViewById(R.id.btnCreate);
		Button btnClose = (Button) findViewById(R.id.btnCancel);

		final RadioButton rbn2 = (RadioButton) findViewById(R.id.rbn2);
		final RadioButton rbn3 = (RadioButton) findViewById(R.id.rbn3);
		final RadioButton rbn4 = (RadioButton) findViewById(R.id.rbn4);
		final RadioButton rbn5 = (RadioButton) findViewById(R.id.rbn5);

		rbn2.isChecked();
		edt_answer_1.setVisibility(View.VISIBLE);
		edt_answer_1.setVisibility(View.VISIBLE);
		edt_answer_3.setVisibility(View.INVISIBLE);
		edt_answer_4.setVisibility(View.INVISIBLE);
		edt_answer_5.setVisibility(View.INVISIBLE);

		txtA.setVisibility(View.VISIBLE);
		txtB.setVisibility(View.VISIBLE);
		txtC.setVisibility(View.INVISIBLE);
		txtD.setVisibility(View.INVISIBLE);
		txtE.setVisibility(View.INVISIBLE);

		rbn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				edt_answer_1.setVisibility(View.VISIBLE);
				edt_answer_1.setVisibility(View.VISIBLE);
				edt_answer_3.setVisibility(View.INVISIBLE);
				edt_answer_4.setVisibility(View.INVISIBLE);
				edt_answer_5.setVisibility(View.INVISIBLE);

				txtA.setVisibility(View.VISIBLE);
				txtB.setVisibility(View.VISIBLE);
				txtC.setVisibility(View.INVISIBLE);
				txtD.setVisibility(View.INVISIBLE);
				txtE.setVisibility(View.INVISIBLE);

			}
		});

		rbn3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				edt_answer_1.setVisibility(View.VISIBLE);
				edt_answer_1.setVisibility(View.VISIBLE);
				edt_answer_3.setVisibility(View.VISIBLE);
				edt_answer_4.setVisibility(View.INVISIBLE);
				edt_answer_5.setVisibility(View.INVISIBLE);

				txtA.setVisibility(View.VISIBLE);
				txtB.setVisibility(View.VISIBLE);
				txtC.setVisibility(View.VISIBLE);
				txtD.setVisibility(View.INVISIBLE);
				txtE.setVisibility(View.INVISIBLE);

			}
		});

		rbn4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				edt_answer_1.setVisibility(View.VISIBLE);
				edt_answer_1.setVisibility(View.VISIBLE);
				edt_answer_3.setVisibility(View.VISIBLE);
				edt_answer_4.setVisibility(View.VISIBLE);
				edt_answer_5.setVisibility(View.INVISIBLE);

				txtA.setVisibility(View.VISIBLE);
				txtB.setVisibility(View.VISIBLE);
				txtC.setVisibility(View.VISIBLE);
				txtD.setVisibility(View.VISIBLE);
				txtE.setVisibility(View.INVISIBLE);
			}
		});

		rbn5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				edt_answer_1.setVisibility(View.VISIBLE);
				edt_answer_1.setVisibility(View.VISIBLE);
				edt_answer_3.setVisibility(View.VISIBLE);
				edt_answer_4.setVisibility(View.VISIBLE);
				edt_answer_5.setVisibility(View.VISIBLE);

				txtA.setVisibility(View.VISIBLE);
				txtB.setVisibility(View.VISIBLE);
				txtC.setVisibility(View.VISIBLE);
				txtD.setVisibility(View.VISIBLE);
				txtE.setVisibility(View.VISIBLE);
			}
		});

		btnSave.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				title = edt_title.getText().toString();
				question = edt_question.getText().toString();

				String x = question.replaceAll(",", " ");

				if (title.equalsIgnoreCase("") || x.equalsIgnoreCase("")) {
					edt_title.setError("Fill the Title");
					edt_question.setError("Fill the Question");
				} else {
					Calendar c = Calendar.getInstance();
					SimpleDateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
					date = df1.format(c.getTime());

					hour = c.get(Calendar.HOUR_OF_DAY);
					minute = c.get(Calendar.MINUTE);
					second = c.get(Calendar.SECOND);

					String questiontime = hour + ":" + minute + ":" + second;

					answer1 = edt_answer_1.getText().toString();
					answer2 = edt_answer_2.getText().toString();
					answer3 = edt_answer_3.getText().toString();
					answer4 = edt_answer_4.getText().toString();
					answer5 = edt_answer_5.getText().toString();

					qdb.open();
					adb.open();

					String finaldate = date + " " + questiontime;

					format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

					try {
						d1 = format.parse(finaldate);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (rbn2.isChecked()) {
						if (answer1.equalsIgnoreCase("")
								|| answer2.equalsIgnoreCase("")) {
							edt_answer_1.setError("Input Answer");
							edt_answer_2.setError("Input Answer");
							if (answer1.length() > 0)
								edt_answer_1.setError(null);
							if (answer2.length() > 0)
								edt_answer_2.setError(null);
						} else {
							qdb.insertQuestionList(user_id, date, x, course_id,
									title, questiontime, answer_time);

							String is = null;

							cursor = qdb.getQuestion(course_id, x);
							cursor.moveToFirst();

							for (int i = 0; i < cursor.getCount(); i++) {
								for (int j = 0; j < cursor.getColumnCount(); j++) {
									is = cursor.getString(j);
								}
							}

							int foo = Integer.parseInt(is);

							adb.insertAnswerList(user_id, answer1, finaldate,
									foo, course_id, d1.getTime());
							adb.insertAnswerList(user_id, answer2, finaldate,
									foo, course_id, d1.getTime());
							
							Cursor mCursor = qdb.getQuestion(course_id, x);
							mCursor.moveToFirst();
							Question_id = mCursor.getString(0);
							
							Intent in = new Intent(FormAddQuestion.this,
									MainResult.class);
							Bundle bundle = new Bundle();
							bundle.putCharSequence("setQuestionId", Question_id);
							bundle.putCharSequence("setInfoCourse", course_id);
							bundle.putCharSequence("setUserId", user_id);
							in.putExtras(bundle);
							startActivity(in);
							finish();
						}
					}

					if (rbn3.isChecked()) {
						if (answer1.equalsIgnoreCase("")
								|| answer2.equalsIgnoreCase("")
								|| answer3.equalsIgnoreCase("")) {
							edt_answer_1.setError("Input Answer");
							edt_answer_2.setError("Input Answer");
							edt_answer_3.setError("Input Answer");
							if (answer1.length() > 0)
								edt_answer_1.setError(null);
							if (answer2.length() > 0)
								edt_answer_2.setError(null);
							if (answer3.length() > 0)
								edt_answer_3.setError(null);
						} else {
							qdb.insertQuestionList(user_id, date, x, course_id,
									title, questiontime, answer_time);

							String is = null;

							cursor = qdb.getQuestion(course_id, x);
							cursor.moveToFirst();

							for (int i = 0; i < cursor.getCount(); i++) {
								for (int j = 0; j < cursor.getColumnCount(); j++) {
									is = cursor.getString(j);
								}
							}

							int foo = Integer.parseInt(is);

							adb.insertAnswerList(user_id, answer1, finaldate,
									foo, course_id, d1.getTime());
							adb.insertAnswerList(user_id, answer2, finaldate,
									foo, course_id, d1.getTime());
							adb.insertAnswerList(user_id, answer3, finaldate,
									foo, course_id, d1.getTime());
							
							Cursor mCursor = qdb.getQuestion(course_id, x);
							mCursor.moveToFirst();
							Question_id = mCursor.getString(0);
							
							Intent in = new Intent(FormAddQuestion.this,
									MainResult.class);
							Bundle bundle = new Bundle();
							bundle.putCharSequence("setQuestionId", Question_id);
							bundle.putCharSequence("setInfoCourse", course_id);
							bundle.putCharSequence("setUserId", user_id);
							in.putExtras(bundle);
							startActivity(in);
							finish();
						}
					}

					if (rbn4.isChecked()) {
						if (answer1.equalsIgnoreCase("")
								|| answer2.equalsIgnoreCase("")
								|| answer3.equalsIgnoreCase("")
								|| answer4.equalsIgnoreCase("")) {
							edt_answer_1.setError("Input Answer");
							edt_answer_2.setError("Input Answer");
							edt_answer_3.setError("Input Answer");
							edt_answer_4.setError("Input Answer");
							if (answer1.length() > 0)
								edt_answer_1.setError(null);
							if (answer2.length() > 0)
								edt_answer_2.setError(null);
							if (answer3.length() > 0)
								edt_answer_3.setError(null);
							if (answer4.length() > 0)
								edt_answer_4.setError(null);
						} else {
							qdb.insertQuestionList(user_id, date, x, course_id,
									title, questiontime, answer_time);

							String is = null;

							cursor = qdb.getQuestion(course_id, x);
							cursor.moveToFirst();

							for (int i = 0; i < cursor.getCount(); i++) {
								for (int j = 0; j < cursor.getColumnCount(); j++) {
									is = cursor.getString(j);
								}
							}

							int foo = Integer.parseInt(is);

							adb.insertAnswerList(user_id, answer1, finaldate,
									foo, course_id, d1.getTime());
							adb.insertAnswerList(user_id, answer2, finaldate,
									foo, course_id, d1.getTime());
							adb.insertAnswerList(user_id, answer3, finaldate,
									foo, course_id, d1.getTime());
							adb.insertAnswerList(user_id, answer4, finaldate,
									foo, course_id, d1.getTime());
							
							Cursor mCursor = qdb.getQuestion(course_id, x);
							mCursor.moveToFirst();
							Question_id = mCursor.getString(0);
							
							Intent in = new Intent(FormAddQuestion.this,
									MainResult.class);
							Bundle bundle = new Bundle();
							bundle.putCharSequence("setQuestionId", Question_id);
							bundle.putCharSequence("setInfoCourse", course_id);
							bundle.putCharSequence("setUserId", user_id);
							in.putExtras(bundle);
							startActivity(in);
							finish();
						}
					}

					if (rbn5.isChecked()) {
						if (answer1.equalsIgnoreCase("")
								|| answer2.equalsIgnoreCase("")
								|| answer3.equalsIgnoreCase("")
								|| answer4.equalsIgnoreCase("")
								|| answer5.equalsIgnoreCase("")) {
							edt_answer_1.setError("Input Answer");
							edt_answer_2.setError("Input Answer");
							edt_answer_3.setError("Input Answer");
							edt_answer_4.setError("Input Answer");
							edt_answer_5.setError("Input Answer");
							if (answer1.length() > 0)
								edt_answer_1.setError(null);
							if (answer2.length() > 0)
								edt_answer_2.setError(null);
							if (answer3.length() > 0)
								edt_answer_3.setError(null);
							if (answer4.length() > 0)
								edt_answer_4.setError(null);
							if (answer5.length() > 0)
								edt_answer_5.setError(null);
						} else {
							qdb.insertQuestionList(user_id, date, x, course_id,
									title, questiontime, answer_time);

							String is = null;

							cursor = qdb.getQuestion(course_id, x);
							cursor.moveToFirst();

							for (int i = 0; i < cursor.getCount(); i++) {
								for (int j = 0; j < cursor.getColumnCount(); j++) {
									is = cursor.getString(j);
								}
							}

							int foo = Integer.parseInt(is);

							adb.insertAnswerList(user_id, answer1, finaldate,
									foo, course_id, d1.getTime());
							adb.insertAnswerList(user_id, answer2, finaldate,
									foo, course_id, d1.getTime());
							adb.insertAnswerList(user_id, answer3, finaldate,
									foo, course_id, d1.getTime());
							adb.insertAnswerList(user_id, answer4, finaldate,
									foo, course_id, d1.getTime());
							adb.insertAnswerList(user_id, answer5, finaldate,
									foo, course_id, d1.getTime());
							
							Cursor mCursor = qdb.getQuestion(course_id, x);
							mCursor.moveToFirst();
							Question_id = mCursor.getString(0);
							
							Intent in = new Intent(FormAddQuestion.this,
									MainResult.class);
							Bundle bundle = new Bundle();
							bundle.putCharSequence("setQuestionId", Question_id);
							bundle.putCharSequence("setInfoCourse", course_id);
							bundle.putCharSequence("setUserId", user_id);
							in.putExtras(bundle);
							startActivity(in);
							finish();
						}
					}
					
					adb.close();
					qdb.close();

					
				}
			}
		});

		btnClose.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(FormAddQuestion.this, MainQuestion.class);
				Bundle bundle = new Bundle();
				bundle.putCharSequence("setInfoCourse", course_id);
				bundle.putCharSequence("setUserId", user_id);
				i.putExtras(bundle);
				startActivity(i);
				finish();
			}
		});

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			Intent intent = new Intent(this, MainQuestion.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			Bundle bundle = new Bundle();
			bundle.putCharSequence("setInfoCourse", course_id);
			bundle.putCharSequence("setUserId", user_id);
			intent.putExtras(bundle);
			startActivity(intent);
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}